package com.bsj.media.tool;

import com.alibaba.fastjson.JSONObject;
import com.bsj.media.init.NettyUDPIntercomUntil;
import com.bsj.media.init.SendChannelUntil;
import com.bsj.media.network.websocket.CmdDto;
import com.bsj.power.common.def.dto.media.BackVodCtrolParam;
import com.bsj.power.common.def.dto.media.BackVodParam;
import com.bsj.power.common.util.SpringUtil;
import lombok.extern.slf4j.Slf4j;

/**
 * @author yinhao
 * @version 1.0
 * @description 回放控制方法
 * @date 2024/7/31
 */
@Slf4j
public class HttpPlayBackTool {

    static VideoCmdUntil videoCmdUntil;

    public static void init() {
        videoCmdUntil = (VideoCmdUntil) SpringUtil.getBean("videoCmdUntil");
    }

    /**
     * 回放
     * @param cmdDto
     * @param terminalNo
     * @param channelNo
     */
    public static void cmdLivePlayBack(CmdDto cmdDto, String terminalNo, int channelNo) {
        //0是播放，1是关闭
        int videoFlag = cmdDto.getVideoFlag();
        String str = cmdDto.getStr();
        String simChannel = cmdDto.getSimChannel();
        Integer port = cmdDto.getPort();
        //播放
        if (videoFlag == 0) {
            BackVodParam param = JSONObject.parseObject(str, BackVodParam.class);
            Integer backType = param.getBackType();
            //正常打开
            if (backType == null || backType == 0) {
                param.setSpeed(0);
                param.setBackType(0);
            } else {
                //快进回放
            }
            param.setTerminalNo(terminalNo);
            param.setChannelNo(channelNo);
            param.setDataType(0);
            param.setStoreType(0);
            param.setPort(port);
            log.info("发送回放开启指令参数:" + JSONObject.toJSONString(param));
            videoCmdUntil.replayReq(param);
            //关闭videoFlag
        } else if (videoFlag == 1) {
            closePlayBack(cmdDto.getPort(), terminalNo, channelNo);
        }
    }

    /**
     * 回放关闭
     * @param port
     * @param terminalNo
     * @param channelNo
     */
    public static void closePlayBack(Integer port, String terminalNo, int channelNo) {
        //回放关闭
        BackVodCtrolParam backVodParam = new BackVodCtrolParam();
        backVodParam.setTerminalNo(terminalNo);
        backVodParam.setChannelNo(channelNo);
        backVodParam.setControlType(2);
        backVodParam.setControl(2);
//        backVodParam.setPort(port);
        //回放UDP服务端关闭
        String simChannel = terminalNo + "-" + channelNo + "-" + port;
        SendChannelUntil.del(simChannel);
//        NettyUDPIntercomUntil.delPort(Integer.parseInt(terminalNo), port);
        NettyUDPIntercomUntil.del(port);
        log.info("关闭回放指令参数:" + JSONObject.toJSONString(backVodParam));
        videoCmdUntil.replayControlReq(backVodParam);
    }
}
